var h_menu = 99;

var set_interval;
var tempo_troca = 6000;
var qtd_int;
    
$(document).ready(function(){    
    
    $('.go_pag').click(function(){
        pagina = this.href;
        pagina = pagina.replace(/^.*#/, '');

        scrollPagina(pagina);

        return false;
    });
    
    $(window).resize(tela);
    $(window).scroll(scrollTela);
    
    tela();
    scrollTela();
    
    
    $(document).on("submit", "#form_contato",function(){
        
        $.post(URLBASE+'/'+CONTROL+'/contato', $(this).serialize() ,function(data) {
            if(data.ok){
                document.getElementById("form_contato").reset();
                alert('Mensagem enviada com sucesso!');
            } else alert('Ocorreu um erro. Tente novamente mais tarde!');
        }, 'json');
        
        return false;
    });
    
    qtd_int = $('.gw-banner').length;
    if(qtd_int > 1){
        set_interval = setInterval(function(){ 

            var pos = $('.gw-banner:visible').attr('id');
            pos = pos.replace('banner','');
            var pst = pos;

            pos = parseInt(pos) + 1;
            if(pos >= $('.gw-banner').length) pos = 0;

            setBanners(pos,pst);

        },tempo_troca);
    }
    
    $(document).on("click", ".lnkitg a",function(){
        var pst = $('.gw-banner:visible').attr('id');
        pst = pst.replace('banner','');
        var pos = $(this).attr('itemid');
        setBanners(pos,pst); 
    });
    
    $('.lnkitg a').mouseover(function(){
        $('.imgs img').hide();
        $('#'+$(this).attr('itemref')).fadeIn(500);
    }).mouseout(function(){
        $('.imgs img').hide();
    });
});     

function scrollPagina(hash){
    //if($('header').attr('class') == 'qw-header-inicial') h_menu_temp = h_menu + h_inicial;
    //else h_menu_temp = h_menu;
    $('html, body').animate({ scrollTop: ($("#pg-" + hash).offset().top - h_menu ) }, { duration: 1000, queue: false } , function(){ $('html,body').scrollTop($('html,body').scrollTop() + 1); $('html,body').scrollTop($('html,body').scrollTop() - 1); });
}

function tela(){
    var largura = $(window).width();
    var altura = $(window).height();
    
    //$('#pg-iniciall').css({'height': h_menu});
    $('.gw-banner').css({'height': altura - h_menu });
    $('.gw-pagina').css({'min-height': ( altura - h_menu) });
    
    if($(window).height() > 700){
         
    }
    else{

    }
    
}

function scrollTela(){
    var altura = $(window).height() - h_menu;
    var posicao = $(window).scrollTop();
    
    
    if($(window).scrollTop() >= 35){
        $('header').removeClass('qw-header-inicial');
        $('header').addClass('qw-header');
    }else{
        $('header').addClass('qw-header-inicial');
        $('header').removeClass('qw-header');
    }        
} 

function setBanners(escolhido,pst){
    clearTimeout(set_interval);
    $('#banner'+pst).fadeOut(300);// hide();
    $('#banner'+escolhido).fadeIn(800);
    $('.lnkitg a').removeClass('action');
    $('#lnkb'+escolhido).addClass('action');

    var qtd_int = $('.gw-banner').length;    

    if(qtd_int > 1){
        set_interval = setInterval(function(){ 

            var pos = $('.gw-banner:visible').attr('id');
            pos = pos.replace('banner','');
            var pst = pos;

            pos = parseInt(pos) + 1;
            if(pos >= $('.gw-banner').length) pos = 0;

            setBanners(pos,pst);

        },tempo_troca);
    }
}

    
    